package com.atguigu.gulimall.juc.demo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

class ProducerComsumerDemoData{
    private volatile int num = 0;
    private static final Integer max = 6;
    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    Condition producerCondition = lock.newCondition();
    Condition comsumerCondition = lock.newCondition();

    public void in(){
        lock.lock();
        try {
            while (num >= max){
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            num++;
            System.out.println(Thread.currentThread().getName()+"生产了一个蛋糕");
            condition.notifyAll();
        }finally {
            lock.unlock();
        }
    }

    public void out(){
        lock.lock();
        try {
            while(num <= 0){
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            num--;
            System.out.println(Thread.currentThread().getName()+"吃了一个蛋糕");
            condition.notifyAll();
        }finally {
            lock.unlock();
        }
    }


    private volatile int product = 0;

    // 进货
    public void get() {
        lock.lock();

        try {
            while (product >= 6) { // 为了避免虚假唤醒，应该总是使用在循环中。
                System.out.println("产品已满！");

                try {
                    producerCondition.await();
                } catch (InterruptedException e) {
                }

            }
            System.out.println(Thread.currentThread().getName() + " : "
                    + ++product);

            comsumerCondition.signalAll();
        } finally {
            lock.unlock();
        }

    }

    // 卖货
    public void sale() {
        lock.lock();

        try {
            while (product <= 0) {
                System.out.println("缺货！");

                try {
                    comsumerCondition.await();
                } catch (InterruptedException e) {
                }
            }

            System.out.println(Thread.currentThread().getName() + " : "
                    + --product);

            producerCondition.signalAll();

        } finally {
            lock.unlock();
        }
    }
}

public class ProducerComsumerDemo {
    public static void main(String[] args) {
        ProducerComsumerDemoData data = new ProducerComsumerDemoData();
//        for (int i = 0; i < 60; i++) {
//            new Thread(()->{
//                data.in();
//            },String.format("p%s",i)).start();
//        }
//
//        for (int i = 0; i < 60; i++) {
//            new Thread(()->{
//                data.out();
//            },String.format("c%s",i)).start();
//        }

        Productor pro = new Productor(data);
        Consumer con = new Consumer(data);

//        new Thread(pro, "生产者 A").start();
//        new Thread(pro, "生产者 B").start();
//        new Thread(con, "消费者 C").start();
//        new Thread(con, "消费者 D").start();

        for (int i = 0; i < 60; i++) {
            new Thread(pro,String.format("p%s",i)).start();
        }

        for (int i = 0; i < 60; i++) {
            new Thread(con,String.format("c%s",i)).start();
        }


    }
}


// 生产者
class Productor implements Runnable {

    private ProducerComsumerDemoData clerk;

    public Productor(ProducerComsumerDemoData clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            clerk.get();
        }
    }
}

// 消费者
class Consumer implements Runnable {

    private ProducerComsumerDemoData clerk;

    public Consumer(ProducerComsumerDemoData clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            clerk.sale();
        }
    }

}